// pages/adssAdd/adssAdd.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    choose: false,
    name: '',
    phone: '',
    address: '',
    id: null,
    show: false,
  },
  blue(event) {
    this.setData({
      choose: event.detail.value
    })
  },
  setName(options) {
    let name = options.detail.value;
    if (!(/^[\u4E00-\u9FA5A-Za-z0-9]+$/.test(name))) {
      wx.showToast({
        // 中文、英文、数字
        title: '请输入正确的姓名',
        duration: 2000,
        icon: 'none'
      })
    }
    this.setData({
      name
    })

  },
  setPhone(options) {
    let phone = options.detail.value;
    if (!(/^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/.test(phone))) {
      wx.showToast({
        title: '请输入正确的手机号码',
        duration: 2000,
        icon: 'none'
      })
    }
    this.setData({
      phone
    })

  },
  setAddress(options) {
    let address = options.detail.value;
    if (!(/^[\u4E00-\u9FA5A-Za-z0-9]+$/.test(address))) {
      wx.showToast({
        // 中文、英文、数字
        title: '请输入正确收货地址',
        duration: 2000,
        icon: 'none'
      })
    }
    this.setData({
      address
    })
  },

  stay() {
    let list = this.data;
    if (list.address && list.phone && list.name) {
      let adsData = {
        id: new Date().getTime(),
        name: list.name,
        phone: list.phone,
        address: list.address,
        choose: list.choose
      };
      let addressData = wx.getStorageSync('addressData');
      if (addressData) {
        addressData.forEach((item) => {
          if (this.data.choose == true) {
            item.choose = false;
          }
        })
        addressData.push(adsData);
        wx.setStorageSync('addressData', addressData);
      } else {
        wx.setStorageSync('addressData', [adsData]);
      }
      // redirectTo：关闭当前页，跳转到指定页；
      // wx.redirectTo({
      //   url: '/pages/address/address',
      // })
     // 关闭当前页面，返回上一页面或多级页面
     wx.navigateBack({
      delta: 1,
    })
    } else {
      wx.showToast({
        title: '请填入完整信息',
        duration: 2000,
        icon: 'none'
      })
    }
  },
  reset() {
    console.log(this.data.id);
    let addressData = wx.getStorageSync('addressData');
    console.log(addressData);
    addressData.forEach((item, index, arr) => {
      // console.log(item);      
        if (this.data.choose == true) {
          item.choose = false;
        }
      if (item.id == this.data.id) {
        //  console.log( arr[index]);

        arr[index].id = this.data.id;
        arr[index].name = this.data.name;
        arr[index].phone = this.data.phone;
        arr[index].address = this.data.address;
        arr[index].choose = this.data.choose;
      }
    })
    wx.setStorageSync('addressData', addressData);
    // redirectTo：关闭当前页，跳转到指定页；
    // wx.redirectTo({
    //   url: '/pages/address/address',
    // })
    // 关闭当前页面，返回上一页面或多级页面
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options) {
      let arr = options.index.split(',');
      let id = arr[0];
      let show = arr[1];
      console.log(show);
      let address = wx.getStorageSync('addressData');
      let list = [];
      // console.log(address);
      address.forEach((item) => {
        if (item.id == id) {
          list.push(item);
        }
      })
      this.setData({
        choose: list[0].choose,
        name: list[0].name,
        phone: list[0].phone,
        address: list[0].address,
        id: list[0].id,
        show
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (op) {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})